package com.sc.nft.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.*;
import com.sc.nft.controller.vo.WriteOffVO;
import com.sc.nft.dao.PackageRecordDao;
import com.sc.nft.dao.PackageRuleDao;
import com.sc.nft.dao.UserCollectionDao;
import com.sc.nft.entity.PackageRecord;
import com.sc.nft.entity.PackageRule;
import com.sc.nft.entity.UserCollection;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.PledgeTaskInfoService;
import com.sc.nft.service.PledgeTaskVigorousCollectionService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/package")
@Api(tags = "NFT套餐控制器")
public class PackageController {

    private final PackageRuleDao packageRuleDao;
    private final PackageRecordDao packageRecordDao;
    private final UserCollectionDao userCollectionDao;

    @ApiOperation(value = "核销详情", notes = "核销详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/writeOffDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<WriteOffVO> writeOffDetail(@Valid @RequestBody IdRequest request) {
        UserCollection collection = userCollectionDao.getById(request.getId());
        if(ObjectUtil.isNull(collection)|| BooleanUtil.isFalse(collection.getIsHold()) ||collection.getUserId().longValue()!=UserPool.getUser().getId().longValue()){
            return Result.fail(ErrorCode.COLLECTION_NOT_FOUND);
        }
        PackageRule packageRule = packageRuleDao.getOpenByCollectionId(collection.getCollectionId());
        if(ObjectUtil.isNull(packageRule)){
            return Result.fail("藏品未关联套餐");
        }
        WriteOffVO writeOffVO = new WriteOffVO();
        writeOffVO.setPackageName(packageRule.getPackageName());
        writeOffVO.setPackageDescription(packageRule.getPackageDescription());
        writeOffVO.setWriteOffCode(collection.getCollectionUuid());
        writeOffVO.setPackageStartTime(packageRule.getStartTime());
        writeOffVO.setPackageEndTime(packageRule.getEndTime());
        PackageRecord record = packageRecordDao.getByCollectionUuid(collection.getCollectionUuid());
        if(ObjectUtil.isNull(record)){
            if(DateTime.now().isBefore(packageRule.getEndTime())){
                writeOffVO.setStatus(1);
            }else{
                writeOffVO.setStatus(3);
            }
        }else{
            writeOffVO.setStatus(2);
            writeOffVO.setWriteOffRecordTime(record.getRecordTime());
        }
        return Result.success(writeOffVO );
    }

}
